import type { Application, Request, Response } from 'express';
import { configs } from '../../src/config';
import { isAuthenticated } from '../middleware/auth';

interface AppBasicInfo {
  name: string;
  version: string;
}

export const createApiRouter = (server: Application) => {
  const pathPrefix = '/api';

  server.get(`${pathPrefix}/health`, (req: Request, res: Response<AppBasicInfo>) => {
    res.status(200).json({
      name: configs.appName,
      version: configs.appVersion,
    });
  });

  server.get(
    `${pathPrefix}/meta/pages`,
    isAuthenticated,
    (req: Request, res: Response) => {
      res.status(200).json(pageMeta);
    },
  );
};

// TBD: load from file or storage ?
const pageMeta = {
  pages: [
    {
      uri: '/',
      title: 'Dashboard',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [],
    },
    {
      uri: '/register/basic',
      title: 'Basic',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Register Count',
          type: 'bar-line composed chart',
          apiParams: {
            uri: '/beta/mart/onboarding',
            metrics: ['newCount', 'totalCount'],
            dimensions: ['date'],
            dataLimit: -1,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'Country','csc',],
            },
          ],
          dataKeys: [
            {
              chartType: 'bar',
              xAxis: 'date',
              yAxis: 'newCount',
            },
            {
              chartType: 'line',
              xAxis: 'date',
              yAxis: 'totalCount',
            },
          ],
        },
        {
          name: 'Register',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/onboarding',
            metrics: ['newCount', 'totalCount'],
            dimensions: ['date', 'region', 'country', 'modelGroup', 'model','csc'],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'newCount',
            'totalCount',
            'csc'
          ],
        },
      ],
    },
    {
      uri: '/register/compare',
      title: 'Compare',
      subFilters: [
        {
          name: 'Compare by',
          type: 'SingleFixedOptionSelector',
          value: ['Model Series', 'Model Group'],
        },
        {
          name: 'Device',
          type: 'MultiSelector',
          value: [],
        },
      ],
      filterConf: {
        subFilterValueMaxCnt: 5,
        disabledFilters: {
          wearable: ['date'],
          mobile: ['date'],
          pc: ['date'],
          accessory: ['date', 'location'],
        },
      },
      cards: [
        {
          name: 'Register Count Comparison',
          type: 'multi-line chart',
          apiParams: {
            uri: '/beta/mart/onboarding/compare',
            metrics: ['newCount'],
            dimensions: ['date'],
            dataLimit: 5,
          },
          dataKeys: [
            {
              chartType: 'line',
              xAxis: 'date',
              yAxisPrefix: 'model',
            },
          ],
        },
        {
          name: 'Register Comparison',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/onboarding/compare',
            metrics: ['newCount'],
            dimensions: ['date'],
            dataLimit: -1,
          },
          columnKeysPrefix: ['model', 'W'],
        },
      ],
    },
    {
      uri: '/register/color',
      title: 'Color & Storage',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Register Color',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/onboarding/color',
            metrics: ['newCount','totalCount'],
            dimensions: [
              'date',
              'region',
              'country',
              'modelGroup',
              'model',
              'csc',
              'color'
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'csc',
            'color'
          ],
        },
        {
          name: 'Register Storage',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/onboarding/color',
            metrics: ['newCount','totalCount'],
            dimensions: [
              'date',
              'region',
              'country',
              'modelGroup',
              'model',
              'csc',
              'storage'
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'csc',
            'storage'
          ],
        },
      ],
    },
    {
      uri: '/activeness/basic',
      title: 'Basic',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Active Count',
          type: 'stacked bar chart',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount', 'inactiveCount'],
            dimensions: ['date'],
            dataLimit: -1,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'country','csc',],
            },
          ],
          dataKeys: [
            {
              chartType: 'bar',
              xAxis: 'date',
              yAxis: 'activeCount',
            },
            {
              chartType: 'bar',
              xAxis: 'date',
              yAxis: 'inactiveCount',
            },
          ],
        },
        {
          name: 'Active',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount', 'totalCount', 'activeRatio'],
            dimensions: ['date', 'region', 'country', 'modelGroup', 'model','csc'],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'activeCount',
            'totalCount',
            'activeRatio',
            'csc'
          ],
        },
      ],
    },
    {
      uri: '/wearable/color',
      title: 'Color',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Register Color',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/onboarding/color',
            metrics: ['newCount','totalCount'],
            dimensions: [
              'date',
              'region',
              'country',
              'modelGroup',
              'model',
              'csc',
              'color'
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'csc',
            'color'
          ],
        },
      ],
    },
    {
      uri: '/activeness/firmware',
      title: 'Firmware',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Firmware Active Count',
          type: 'stacked area chart',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'fwVersion'],
            dataLimit: 10,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'country','csc'],
            },
          ],
          dataKeys: [
            {
              chartType: 'area',
              xAxis: 'date',
              yAxisPrefix: 'fwVersion',
            },
          ],
        },
        {
          name: 'Active Firmware',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'region', 'country', 'modelGroup', 'model', 'fwVersion','csc'],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'fwVersion',
            'activeCount',
            'csc'
          ],
        },
      ],
    },
    {
      uri: '/activeness/os',
      title: 'OS',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'OS Active Count',
          type: 'stacked area chart',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'osVersion'],
            dataLimit: 10,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'country','csc'],
            },
          ],
          dataKeys: [
            {
              chartType: 'area',
              xAxis: 'date',
              yAxisPrefix: 'osVersion',
            },
          ],
        },
        {
          name: 'Active OS',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'region', 'country', 'modelGroup', 'model', 'osVersion','csc'],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'osVersion',
            'activeCount',
            'csc'
          ],
        },
      ],
    },
    {
      uri: '/activeness/hostOsType',
      title: 'Host OS Type',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Host OS Type Active Count',
          type: 'stacked area chart',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'hostOsType'],
            dataLimit: 10,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'Country'],
            },
          ],
          dataKeys: [
            {
              chartType: 'area',
              xAxis: 'date',
              yAxisPrefix: 'hostOsType',
            },
          ],
        },
        {
          name: 'Active Host OS Type',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: [
              'date',
              'model',
              'hostModelGroup',
              'hostModel',
              'hostOsType',
              'region',
              'country',
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'model',
            'hostModelGroup',
            'hostModel',
            'region',
            'country',
            'hostOsType',
            'activeCount',
          ],
        },
      ],
    },
    {
      uri: '/activeness/hostOsVersion',
      title: 'Host OS Version',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Host OS Version Active Count',
          type: 'stacked area chart',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: ['date', 'hostOsVersion'],
            dataLimit: 10,
          },
          subFilters: [
            {
              name: 'Device',
              type: 'SingleSelector',
              value: ['Model Group', 'Model'],
            },
            {
              name: 'Location',
              type: 'SingleSelector',
              value: ['Region', 'Country'],
            },
          ],
          dataKeys: [
            {
              chartType: 'area',
              xAxis: 'date',
              yAxisPrefix: 'hostOsVersion',
            },
          ],
        },
        {
          name: 'Active Host OS Version',
          type: 'simple table',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount'],
            dimensions: [
              'date',
              'model',
              'hostModelGroup',
              'hostModel',
              'hostOsVersion',
              'region',
              'country',
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'model',
            'hostModelGroup',
            'hostModel',
            'region',
            'country',
            'hostOsVersion',
            'activeCount',
          ],
        },
      ],
    },
    {
      uri: '/activeness/spl',
      title: 'SPL',
      filterConf: {
        disabledFilters: {
          accessory: ['location'],
        },
      },
      cards: [
        {
          name: 'Active Security Patch Level and Active Count for Mobile B2B',
          type: 'simple table spl',
          apiParams: {
            uri: '/beta/mart/activeness',
            metrics: ['activeCount', 'totalCount', 'activeRatio'],
            dimensions: [
              'date',
              'region',
              'country',
              'modelGroup',
              'model',
              'splVersion',
              'csc'
            ],
            dataLimit: -1,
          },
          columnKeys: [
            'date',
            'region',
            'country',
            'modelGroup',
            'model',
            'splVersion',
            'csc'
          ],
        },
      ],
    },
  ],
};
